package com.demo.controller.article;

import com.demo.common.kit.SensitiveWordsKit;
import com.jfinal.core.Controller;
import com.jfinal.validate.Validator;

/**
 * @Auther: 10091
 * @Date: 2018-07-16 15:44
 * @Description:
 */
public class ArticleValidator extends Validator {
    @Override
    protected void validate(Controller c) {
        setShortCircuit(true);
        checkSensitiveWords(c.getPara("article.mainTitle"), "标题 title 包含敏感词");
        checkSensitiveWords(c.getPara("content"), "内容 content 名包含敏感词");

        validateString("article.mainTitle", 3, 100, "msg", "标题长度要求在3到100个字符");
        validateString("content", 3, 999999999, "msg", "正文内容太少啦，多写点哈");
    }

    private void checkSensitiveWords(String value, String msg) {
        if (SensitiveWordsKit.checkSensitiveWord(value) != null) {
            addError("msg", msg);
        }
    }

    @Override
    protected void handleError(Controller controller) {
        controller.renderJson();
    }
}
